/*智能指针本质就是一个类模板，可以创建任意类型的指针对象
当指针对象使用完后会自动调用析构函数*/
#include <bits/stdc++.h>
using namespace std;

template <typename T>
class SmartPtr{
public:
    SmartPtr():m_data(nullptr){}
    SmartPtr(T* data):m_data(data){}
    ~SmartPtr(){
        if(m_data != nullptr){
            delete m_data;
            m_data = nullptr;
        }
    }
    T* operator -> (){
        return m_data;
    } 
    T& operator * (){
        return *m_data;
    } 
private:
    T* m_data;
};

class Test{
public:
    Test() = default;
    ~Test(){
        std::cout<<"test is deleted"<<std::endl;
    }
    void name(const string& name){
        m_name = name;
    }
    string name() const{
        return m_name;
    }

private:
    string m_name;
};

int main(){

    auto p = new Test();
    SmartPtr<Test> sp(p);
    //print:test is deleted
    p->name("jack");
    cout<<p->name()<<endl;
    sp->name("tom");
    cout<<sp->name()<<endl;
    return 0;
}